package com.hnu.store.controller;


import com.hnu.store.dto.NftDTO;
import com.hnu.store.dto.SaleDTO;
import com.hnu.store.entity.Sale;
import com.hnu.store.service.ISaleService;
import com.hnu.store.utils.JsonResult;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 售单的Controller
 */
@RestController
@CrossOrigin

@RequestMapping("sale")
public class SaleController extends BaseController{

    @Autowired
    private ISaleService saleService;

    /**
     * 根据用户id获取售单列表
     * @param userId 用户id
     * @param stringPara 关键字
     * @param status 状态
     * @return json
     */

    @RequestMapping("getSaleListById")
    public JsonResult getSaleListById(Integer userId,String stringPara,String status){
        List<SaleDTO> saleList=saleService.getSaleListById(userId,stringPara,status);
        JsonResult result=new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 根据saleId获取sale
     * @param saleId 售单id
     * @return json
     */
    @RequestMapping("getSaleById")
    public JsonResult getSaleById(Integer saleId){
        SaleDTO data=saleService.getSaleById(saleId);

        JsonResult result=new JsonResult(OK);
        result.addData("sale",data);
        return result;
    }

    /**
     * 获取全部售单（除了停售和所有权转让的售单）
     * 用于商城展示
     * @return 返回含有全部售单的json
     */
    @RequestMapping("getAllSale")
    public JsonResult getAllSale(){
        List<SaleDTO> saleList=saleService.getAllSaleList();
        JsonResult result=new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 添加售单
     * @param sale 售单部分信息
     * @return json
     */
    @RequestMapping("addSale")
    public JsonResult addSale(Sale sale) {
        saleService.addSale(sale);
        return new JsonResult(OK);
    }

    /**
     * 获取售单封面，展示在商城
     * @param filePath 文件路径
     * @param fileName 文件名称
     * @param response http的response
     */
    @RequestMapping("getImg")
    public void getImg(String filePath,String fileName, HttpServletResponse response) {
        saleService.getImg(filePath,fileName,response);
    }

    /**
     * 更改售单的状态（停售等）
     * @param id 售单id
     * @param status 状态
     * @return json
     */
    @RequestMapping("changeStatus")
    public JsonResult changeStatus(Integer id,Integer status) {
        Sale data = saleService.changeStatus(id,status);
        JsonResult result = new JsonResult(OK);
        result.addData("sale",data);
        return result;
    }

    /**
     * 获取用户可以出售的nft列表
     * @param userId 用户id
     * @return 带有列表的json
     */
    @RequestMapping("getNftList")
    public JsonResult getNftList(Integer userId) {
        List<NftDTO> data = saleService.getNftList(userId);
        JsonResult result = new JsonResult(OK);
        result.addData("nftList",data);
        return result;
    }

    /**
     * 删除售单
     * @param id 售单id
     * @return json
     */
    @RequestMapping("deleteSale")
    public JsonResult deleteSale(Integer id) {
        saleService.deleteSale(id);
        return new JsonResult(OK);
    }

    /**
     * 编辑售单
     * @param sale 修改后的售单
     * @return json
     */
    @RequestMapping("editSale")
    public JsonResult editSale(Sale sale) {
        saleService.editSale(sale);
        return new JsonResult(OK);
    }

    /**
     * 管理员获取全部售单（包括停售的售单）
     * 用于给管理员管理停售
     * @return 售单
     */
    @RequestMapping("adminGetAllSale")
    public JsonResult adminGetAllSale() {
        List<SaleDTO> saleList = saleService.adminGetAllSale();
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 根据商品类型筛选
     * @param goodsType 商品类型（字符串）
     * @return 返回相应商品
     */
    @RequestMapping("getSaleByGoodsType")
    public JsonResult getSaleByGoodsType(Integer id,String goodsType) {
        List<SaleDTO> saleList = saleService.getSaleByGoodsType(id,goodsType);
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 根据版权类型筛选
     * @param saleType 版权类型
     * @return 返回相应商品
     */
    @RequestMapping("getSaleBySaleType")
    public JsonResult getSaleBySaleType(Integer id,String saleType) {
        List<SaleDTO> saleList = saleService.getSaleBySaleType(id,saleType);
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 查询商品，标题和简介中模糊查询
     * @param sortPara 关键字
     * @return 相应商品
     */
    @RequestMapping("search")
    public JsonResult search(Integer id,String sortPara) {
        List<SaleDTO> saleList = saleService.search(id,sortPara);
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 推送商品
     * @param userId 用户id
     * @return 返回推送的商品列表
     */
    @RequestMapping("post")
    public JsonResult post(int userId) {
        List<SaleDTO> saleList = saleService.post(userId);
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleList);
        return result;
    }

    /**
     * 商城展示商品
     * @param id 用户id
     * @return 返回商品列表
     */
    @RequestMapping("saleForMall")
    public JsonResult saleForMall(int id) {
        List<SaleDTO> saleDTOList = saleService.saleForMall(id);
        JsonResult result = new JsonResult(OK);
        result.addData("saleList",saleDTOList);
        return result;
    }
}
